package com.cyh.testcustomui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class ShapeLoadingView extends LinearLayout {
    private ShapeView id_shape_view;
    private View v_shadow_bg;

    private int mTranslationDistance = 0;
    private final long mAnimationDuration = 500;
    public ShapeLoadingView(Context context) {
        this(context, null);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTranslationDistance = MyUtil.dp2px(getContext(), 80);
        initLayout();
    }

    private void initLayout() {
        //加载布局
        //View loadView = inflate(getContext(), R.layout.ui_loading_view, null);
        //addView(loadView);

        //上面两句可以用下面一句代替
        //this 代表把 ui_loading_view布局加载到ShapeLoadingView自定义控件中。
        inflate(getContext(), R.layout.ui_loading_view, this);

        id_shape_view = findViewById(R.id.id_shape_view);
        v_shadow_bg = findViewById(R.id.v_shadow_bg);

        //开始下落动画
        startFallAnimator();
    }

    private void startFallAnimator() {
        //translationY是怎么获得的?
        //id_shape_view.setTranslationY(); 复制出translationY字符串就可以了
        ObjectAnimator translaionAnimator = ObjectAnimator.ofFloat(id_shape_view, "translationY", 0, mTranslationDistance);
        translaionAnimator.setDuration(mAnimationDuration);
        //translaionAnimator.start();

        //配合阴影缩小
        ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(v_shadow_bg, "scaleX", 1f, 0.3f);
        scaleAnimator.setDuration(mAnimationDuration);
        //scaleAnimator.start();


        //组合动画
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(translaionAnimator, scaleAnimator);
        //animatorSet.playSequentially(); //按照放入的顺序，先后执行
        animatorSet.setInterpolator(new AccelerateInterpolator());


        //监听动画结束
        //new Animator.AnimatorListener(), 里面要实现的方法太多，所以改用实现下面的方法
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                id_shape_view.exchange();

                //上抛动画
                startUpAnimator();


            }
        });

        animatorSet.start();
    }

    private void startUpAnimator() {
        //translationY是怎么获得的?
        //id_shape_view.setTranslationY(); 复制出translationY字符串就可以了
        ObjectAnimator translaionAnimator = ObjectAnimator.ofFloat(id_shape_view, "translationY", mTranslationDistance, 0);
        //translaionAnimator.setDuration(mAnimationDuration);
        //translaionAnimator.start();

        //配合阴影缩小
        ObjectAnimator scaleAnimator = ObjectAnimator.ofFloat(v_shadow_bg, "scaleX", 0.3f, 1f);
        //scaleAnimator.setDuration(mAnimationDuration);
        //scaleAnimator.start();


        //组合动画
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(translaionAnimator, scaleAnimator);
        //animatorSet.playSequentially(); //按照放入的顺序，先后执行
        animatorSet.setDuration(mAnimationDuration);
        animatorSet.setInterpolator(new DecelerateInterpolator());


        //监听动画结束
        //new Animator.AnimatorListener(), 里面要实现的方法太多，所以改用实现下面的方法
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                //下落动画
                startFallAnimator();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);

                //开始上抛时，旋转
                startRotationAnimator();
            }
        });

        //加入旋转时， start必须在监听的后面
        animatorSet.start();
    }

    private void startRotationAnimator() {
        ObjectAnimator rotationAnimator = null;
        switch (id_shape_view.getmCurrentShape()) {
            case Circle:
            case Square:
                rotationAnimator = ObjectAnimator.ofFloat(id_shape_view, "rotation", 0, 180);
                break;
            case Triangle:
                rotationAnimator = ObjectAnimator.ofFloat(id_shape_view, "rotation", 0, -120);
                break;
        }
        rotationAnimator.setDuration(mAnimationDuration);
        rotationAnimator.setInterpolator(new DecelerateInterpolator());
        rotationAnimator.start();
    }

}
